package com.kun.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.kun.entity.Activity;
import com.kun.entity.ActivityExample;
import com.kun.entity.ActivityExample.Criteria;
import com.kun.mapper.cluster.ClusterActivityMapper;
import com.kun.mapper.master.MasterActivityMapper;
import com.kun.service.ActivityService;
import com.kun.util.PaginationUtil;

/**
 * @description: 
 * @author: KM
 * 2018年5月3日 下午4:50:47
 */
@Service
public class ActivityServiceImpl implements ActivityService {
	
	@Autowired
	private ClusterActivityMapper clusterActivityMapper;
	
	@Autowired
	private MasterActivityMapper masterActivityMapper;
	
	/**
	 * 展示活动标题
	 * */
	public Map<String,Object> selectActivityTiltle(int begin,int length,String jsonString,boolean isShow){
		ActivityExample example = new ActivityExample();
		String order = "";
		Criteria c = example.createCriteria();
		if(isShow==false){
			c.andIsShowEqualTo("1");
			order = "is_recommend desc";
		}
		if(!"".equals(jsonString)&&jsonString!=null&&jsonString.length()>0){
			JSONObject json = JSONObject.parseObject(jsonString);
			@SuppressWarnings("rawtypes")
			Map cMap = (Map)json;
			if (cMap != null && !cMap.isEmpty()) {
				@SuppressWarnings("rawtypes")
				Iterator it = cMap.keySet().iterator();
				while(it.hasNext()){
					String name = (String)it.next();
					if(!cMap.get(name).equals(null)){
						if(name.equals("title")){//标题查询
							c.andTitleLike("%" + cMap.get(name) + "%");
						}else if(name.equals("issueTime")){//发表时间 排序
							if(!"".equals(order)){
								order = order + ",";
							}
							order = order + "issue_time  "+cMap.get(name); 
						}
					}
				}
			}
		}
		example.setOrderByClause(order);
		int allRecord = clusterActivityMapper.countByExample(example);
		PaginationUtil pagination = new PaginationUtil((begin-1)*length,length,allRecord);
		example.setPagination(pagination);
		
		List<Activity> activityList = clusterActivityMapper.selectByExample(example);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("allRecord", allRecord);
		resultMap.put("activityList", activityList);
		return resultMap;
	}
	
	/**
	 * 根据id查询活动
	 * */
	public Activity selectActivityByKey(int activityId){
		return clusterActivityMapper.selectByPrimaryKey(activityId);
	}

	public List<Activity> selectAllActivity(){
		ActivityExample example = new ActivityExample();
		example.setOrderByClause("issue_time desc");
		return clusterActivityMapper.selectByExample(example);
	}
	
	/**
	 * 更新普通活动信息
	 * */
	public int updateSimpleActivityByKey(Activity activity){
		return masterActivityMapper.updateByPrimaryKeySelective(activity);
	}
	
	/**
	 * 更新活动内容信息
	 * */
	public int updateActivityByKey(Activity activity){
		return masterActivityMapper.updateByPrimaryKeyWithBLOBs(activity);
	}
	
	/**
	 * 添加活动
	 * */
	public int insertActivity(Activity activity){
		return masterActivityMapper.insert(activity);
	}
	
	/**
	 * 删除活动
	 * */
	public int deleteActivityByKey(int id){
		return masterActivityMapper.deleteByPrimaryKey(id);
	}
}
